<?php

namespace app\data\command;

use think\admin\Command;
use think\console\Input;
use think\console\Output;

/**
 * 数据统计-汇总
 * Class ReportObservation
 * @package app\data\command
 */
class ReportObservation extends Command
{
    protected function configure()
    {
        $this->setName('report_observation');
        $this->setDescription('数据统计-汇总');
    }

    protected function execute(Input $input, Output $output)
    {
        $yesterday = date('Y-m-d', strtotime('-1 day'));
        $this->app->db->name('report_observation')->where('date', $yesterday)->delete();

        $yesterdayMin = $yesterday . ' 00:00:00';
        $yesterdayMax = $yesterday . ' 23:59:59';
        $dockingCountTotal = 0;
        $paintCountTotal = 0;
        $unPaintCountTotal = 0;
        $driverReasonCountTotal = 0;
        $todayPaintCount = 0;

        foreach ($this->app->db->name('data_record')
                     ->where(function ($query) use ($yesterdayMin, $yesterdayMax) {
                         $query->where('time_docking', '>=', $yesterdayMin)
                             ->where('time_docking', '<=', $yesterdayMax);
                     })
                     ->whereOr(function ($query) use ($yesterdayMin, $yesterdayMax) {
                         $query->where('time_complete', '>=', $yesterdayMin)
                             ->where('time_complete', '<=', $yesterdayMax);
                     })
                     ->cursor() as $vo) {
            $this->createReports($reports, $vo['province'], $vo['city'], $vo);
        }

        if (!empty($reports)) {
            foreach ($reports as $province => $report) {
                foreach ($report as $city => $reportItem) {
                    $insertData[] = [
                        'date' => $yesterday,
                        'province' => $province,
                        'city' => $city,
                        'docking_count' => $reportItem['docking_count'],
                        'paint_count' => $reportItem['paint_count'],
                        'un_paint_count' => $reportItem['un_paint_count'],
                        'driver_reason_count' => $reportItem['driver_reason_count'],
                        'day_rate' => bcmul($reportItem['docking_count'] ? bcdiv($reportItem['today_paint_count'], $reportItem['docking_count'], 4) : 0, 100, 2),
                        'month_rate' => 0,
                    ];

                    $dockingCountTotal += $reportItem['docking_count'];
                    $paintCountTotal += $reportItem['paint_count'];
                    $unPaintCountTotal += $reportItem['un_paint_count'];
                    $driverReasonCountTotal += $reportItem['driver_reason_count'];
                    $todayPaintCount += $reportItem['today_paint_count'];
                }
            }
        }

        if (!empty($insertData)) {
            $insertTotal = [
                'date' => $yesterday,
                'province' => '合计',
                'city' => '合计',
                'docking_count' => $dockingCountTotal,
                'paint_count' => $paintCountTotal,
                'un_paint_count' => $unPaintCountTotal,
                'driver_reason_count' => $driverReasonCountTotal,
                'day_rate' => bcmul($dockingCountTotal ? bcdiv($todayPaintCount, $dockingCountTotal, 4) : 0, 100, 2),
                'month_rate' => 0,
            ];
            $this->app->db->name('report_observation')->insert($insertTotal);
            $this->app->db->name('report_observation')->insertAll($insertData);
        }
    }

    /**
     * 生成统计
     * @param $province
     * @param $city
     * @param $data
     * @return array
     */
    public function createReports(&$reports, $province, $city, $data)
    {
        if (empty($reports[$province][$city]['docking_count'])) {
            $reports[$province][$city]['docking_count'] = 0;
        }
        if (empty($reports[$province][$city]['paint_count'])) {
            $reports[$province][$city]['paint_count'] = 0;
        }
        if (empty($reports[$province][$city]['un_paint_count'])) {
            $reports[$province][$city]['un_paint_count'] = 0;
        }
        if (empty($reports[$province][$city]['driver_reason_count'])) {
            $reports[$province][$city]['driver_reason_count'] = 0;
        }
        if (empty($reports[$province][$city]['today_paint_count'])) {
            $reports[$province][$city]['today_paint_count'] = 0;
        }

        $yesterday = date('Y-m-d', strtotime('-1 day'));
        if ($data['time_docking'] && date('Y-m-d', strtotime($data['time_docking']) == $yesterday)) {
            $reports[$province][$city]['docking_count'] += 1;
        }

        if ($data['time_complete'] && date('Y-m-d', strtotime($data['time_complete']) == $yesterday)) {
            if (date('Y-m-d', strtotime($data['time_docking']) == $yesterday)) {
                $reports[$province][$city]['today_paint_count'] += 1;
            }
            $reports[$province][$city]['paint_count'] += 1;
        } else {
            $reports[$province][$city]['un_paint_count'] += 1;

            if ($data['driver_reason']) {
                $reports[$province][$city]['driver_reason_count'] += 1;
            }
        }
    }

}